//
//  SPUpcomingCenterTableViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/19.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPUpcomingCenterTableViewController.h"
#import "SPCommon.h"
#import "SPUpcomingHeaderView.h"
#import "SPLiveTableViewCell.h"
#import "SPOrderTableViewCell.h"
#import "SPUpcomingSectionHeaderView.h"
#import "SPNewsTableViewController.h"
#import "SPRechargeViewController.h"
#import "SPOrderViewController.h"
#import "SPLiveStatusTableViewController.h"
#import "SPLiveListViewController.h"
#import "SPWattingSendDetailViewController.h"
#import "SPAfterSaleDetailViewController.h"
#import "SPWriteOrderInfoViewController.h"
#import "SPLiveListModel.h"
#import "SPOrderListModel.h"


@interface SPUpcomingCenterTableViewController ()
@property (strong, nonatomic) UILabel *redhot;
@property (strong, nonatomic) NSArray<NSString *> *sections;
@property (nonatomic,strong) SPUpcomingHeaderView *headerV;
@property (nonatomic,strong) NSArray<SPLiveListModel *> *list_live;
@property (nonatomic,strong) NSArray<SPOrderListModel *> *list_order;

@end

@implementation SPUpcomingCenterTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _sections = @[@"直播",@"订单处理"];
    [self setupBtn];
    [self setupTableView];
    [self fetchBalance];
    [self fetchLiveData];
    [self fetchOrderData];
}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.tableView.tableHeaderView.height = 90;
}


- (void)setupTableView
{
    SPUpcomingHeaderView *headerV = [SPUpcomingHeaderView shareHeaderView];
    _headerV = headerV;
    headerV.rechargeBtnOnClickBlock = ^{
        [self rechargeBtnOnClick];
    };
    self.tableView.tableHeaderView = headerV;
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableFooterView.height  =10;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPLiveTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPLiveTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPOrderTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPOrderTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPUpcomingSectionHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass([SPUpcomingSectionHeaderView class])];
}


//设置导航栏右边按钮
- (void)setupBtn
{
    UIButton *newsBtn = [[UIButton alloc] init];
    newsBtn.bounds = CGRectMake(0, 0, 32, 32);
    [newsBtn addTarget:self action:@selector(newsBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [newsBtn setImage:[UIImage imageNamed:@"icon_news"] forState:UIControlStateNormal];
    UILabel *redhot = [UILabel new];
    _redhot = redhot;
    redhot.textColor = [UIColor whiteColor];
    redhot.font = [UIFont systemFontOfSize:7];
    redhot.textAlignment = NSTextAlignmentCenter;
    redhot.text = @"2";
    redhot.backgroundColor = RZ_COLOR(255, 48, 74);
    redhot.frame = CGRectMake(18, 4, 9, 9);
    redhot.layer.cornerRadius = 4.0f;
    redhot.clipsToBounds = YES;
    [newsBtn addSubview:redhot];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newsBtn];
    
}




#pragma mark--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if(section == 0){
        count = self.list_live.count;
    }else{
        count = self.list_order.count;
    }
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 55;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.section == 0?110:140;
    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPUpcomingSectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SPUpcomingSectionHeaderView class])];
    if(section == 0){
        headerV.title.text = [NSString stringWithFormat:@"直播(%ld)",(long)self.list_live.count];
    }else{
        headerV.title.text = [NSString stringWithFormat:@"订单处理(%ld)",self.list_order.count];
    }
    
    headerV.moreBtnOnClickBlock = ^{
        [self moreWithTag:section];
    };
    return headerV;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if(indexPath.section == 0){
        SPLiveTableViewCell *liveCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPLiveTableViewCell class])];
        switch (indexPath.row) {
            case 0:
                liveCell.type = LiveType_JJKB;
                break;
            case 1:
                liveCell.type = LiveType_ZZZB;
                break;
        }
        cell = liveCell;
        
    }else{
        
        SPOrderTableViewCell *orderCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPOrderTableViewCell class])];
        
        __weak typeof(self) weakSelf = self;
        orderCell.moreBtnOnClickBlock = ^(SPOrderListModel *model) {
            [weakSelf moreWithOrderModel:model];
        };
        orderCell.handleBtnOnClickBlock = ^(SPOrderListModel *model) {
            [weakSelf handleWithOrder:model];
        };
        
        orderCell.model = self.list_order[indexPath.row];
        cell = orderCell;
    }
    return cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        LiveType type = indexPath.row==0?LiveType_JJKB:LiveType_ZZZB;
        SPLiveStatusTableViewController *status = [[SPLiveStatusTableViewController alloc] initWithLiveType:type];
        [self.navigationController pushViewController:status animated:YES];
        
    }else{
        
    }
}



#pragma mark-- 查看更多 直播||订单处理
- (void)moreWithTag:(NSInteger)tag
{
    if(tag==0){
        SPLiveListViewController *liveList = [[SPLiveListViewController alloc] init];
        [self.navigationController pushViewController:liveList animated:YES];
    }else{
        SPOrderViewController *order = [[SPOrderViewController alloc] initWithType:OrderType_DFH];
        [self.navigationController pushViewController:order animated:YES];
    }
    
}


#pragma mark--订单处理查看更多
- (void)moreWithOrderModel:(SPOrderListModel *)model
{
    if(model.status == 1){
        SPWattingSendDetailViewController *ws = [[SPWattingSendDetailViewController alloc] initWithOrder_id:model.order_id];
        [self.navigationController pushViewController:ws animated:YES];
    }else{
        SPAfterSaleDetailViewController *as = [[SPAfterSaleDetailViewController alloc] initWithOrder_id:model.order_id];
        [self.navigationController pushViewController:as animated:YES];
    }
}


#pragma mark--订单处理
- (void)handleWithOrder:(SPOrderListModel *)model
{
    OrderType type = (model.status == 1)?OrderType_DFH:OrderType_SH;
    SPWriteOrderInfoViewController *wirte = [[SPWriteOrderInfoViewController alloc] initWithOrder_id:model.order_id type:type];
    [self.navigationController pushViewController:wirte animated:YES];
}





#pragma mark--查看新消息
- (void)newsBtnClick:(UIButton *)btn
{
    [self.navigationController pushViewController:[SPNewsTableViewController new] animated:YES];
}

#pragma mark--立即充值
- (void)rechargeBtnOnClick
{
    [self.navigationController pushViewController:[SPRechargeViewController new] animated:YES];
}



#pragma mark--直播数据
- (void)fetchLiveData
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=dai_live&token=568e86c8630f7b44f29a23ba211d9f25
     参数 page 第几页 psize 每页多少条 默认5
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"dai_live";
    dict[@"page"] = @(1);
    dict[@"psize"] = @(2);
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            
            self.list_live = [SPLiveListModel mj_objectArrayWithKeyValuesArray:res[@"data"][@"acts"]];
           [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}



#pragma mark--订单数据
- (void)fetchOrderData
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=dai_order&token=568e86c8630f7b44f29a23ba211d9f25
     参数 page 第几页 psize 每页多少条 默认5
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"dai_order";
    dict[@"page"] = @(1);
    dict[@"psize"] = @(2);
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            
           self.list_order = [SPOrderListModel mj_objectArrayWithKeyValuesArray:res[@"data"][@"order"]];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}



#pragma mark--余额
- (void)fetchBalance
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=balance&token=568e86c8630f7b44f29a23ba211d9f25
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"balance";
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            
            NSDictionary *obj = [NSDictionary changeType:res[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{//更新
                self.headerV.dict = obj;
            });
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}

@end
